// pages/reward/reward.js
var util = require('../../utils/util.js');
var utilMd5 = require('../../utils/md5.js');
var topicid = 0;
var topictitle = '';
var app = getApp();
var postuid = 0;
var loginuid = 0;
var navtitle = '';
Page({
  data:{
    uid: 0, //当前登录用户uid
    postuser: {}, //当前主题作者uid
    token:'',
    loginUname:'',
    openid:'', //当前登录用户
    postuser:'',
    money: 2, //赞赏金额
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数
    postuid = options.postuid || 0;
    topicid = options.topicid || 0;
    topictitle = options.topictitle || '';
    var inviteuid = options.inviteuid;
    var that = this;
    //设置作者用户信息
    app.GetUserInfo(postuid, function(res){       
      //更新数据
      that.setData({
        postuser:res
      })
      //设置导航标题
      navtitle = '赞赏'+ res.nickname;
      wx.setNavigationBarTitle({
          title: navtitle
      });
      
    });

    //当前登录用户信息
    app.LoginUserInfo(function(res){
      //更新数据
      that.setData({
        uid:res.uid,
        token: res.token,
        loginUname:res.uname,
        openid:res.wxsmall_openid
      });
    })
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  //选择金额
  selectMoney: function(e) {
    var money = e.currentTarget.dataset.money;
      this.setData({
        money: money
      })
  },
  //输入金额
  changeMoney: function(e) {
      var money = e.detail.value;
      this.setData({
        money: money
      })
  },
  //确认赞赏
  reward: function(e) {
    var that = this;
      var uid = e.currentTarget.dataset.uid;
      var openid = e.currentTarget.dataset.openid;
      var uname = e.currentTarget.dataset.uname;
      var token = e.currentTarget.dataset.token;
      var total_fee = e.currentTarget.dataset.money;
      var privatekey = utilMd5.hexMD5(uid+'wxsmallpay'+token);
      if(total_fee <= 0) {
        app.dialog('请输入合法金额', 'loading');
      }
      var data={
            publickey:util.publiccode,
            ac:"wxsmallpay",
            subject:uname+'赞赏了'+topictitle,
            total_fee:total_fee,
            type: 'reward',
            pid:topicid,
            uid:uid,
            openid:openid,
            privatekey: privatekey
        };
        console.log(data);
      //下单请求
    util.api({
        data:data,
        method:'POST',
        success:function(responseData){
            var data = responseData.data;
                            
                console.log(data);
            if(data.res === true) {
                //挂起微信支付请求
                var orderData = data.data;
                wx.requestPayment(
                    {
                        'timeStamp': orderData.timeStamp,
                        'nonceStr': orderData.nonceStr,
                        'package': orderData.package,
                        'signType': orderData.signType,
                        'paySign': orderData.paySign,
                        'success': function (res) {
                            app.dialog('赞赏成功', 'success', function() {
                              wx.redirectTo({
                                url:'/pages/detail/detail?topicid='+topicid
                              })
                            });
                        },
                        'fail': function (res) {
                            console.log(res); 
                            if(res.errMsg=='requestPayment:fail cancel') {

                            } else {
                                app.dialog('赞赏失败', 'loading');
                            }                            
                        }
                    });              
            } else {
                app.dialog('赞赏失败', 'loading');
            }
        }
    });
  },  
  goAtherPage: function(e){
      var url = e.currentTarget.dataset.link;
      wx.navigateTo({url: url})
  },
  //页面分享
  onShareAppMessage: function () {
    return app.sharePage({
      title: navtitle,
      path: '/pages/online-studio/online-studio?uid='+postuid
    });
  }
})